<template>
  <div>
    <el-row :gutter="10">
      <el-col :span="8">
        <el-card>
          <div style="width: 100%; height: 400px" id="pie"></div>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card>
          <div style="width: 100%; height: 400px" id="line"></div>
        </el-card>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import * as echarts from 'echarts'
import {charts, login} from "@/api/house";
import axios from "axios";


const option1 = {
  title: {
    text: '人员性别统计',
    subtext: '比例图',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      type: 'pie',
      center: ['50%', '60%'],
      radius: '50%',
      data: [],
      label: {
        show: true,
        formatter(param) {
          return param.name + ' (' + param.percent + '%)';
        }
      },
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
}


const option2 = {
  title: {
    text: '一周天气变化折线图'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {},
  toolbox: {
    show: true,
    feature: {
      dataZoom: {
        yAxisIndex: 'none'
      },
      dataView: {readOnly: false},
      magicType: {type: ['line', 'bar']},
      restore: {},
      saveAsImage: {}
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: []
  },
  yAxis: {
    type: 'value',
    axisLabel: {
      formatter: '{value} °C'
    }
  },
  series: [
    {
      name: 'Highest',
      type: 'line',
      data: [],
      markPoint: {
        data: [
          {type: 'max', name: 'Max'},
          {type: 'min', name: 'Min'}
        ]
      },
      markLine: {
        data: [{type: 'average', name: 'Avg'}]
      }
    },
    {
      name: 'Lowest',
      type: 'line',
      data: [],
      markPoint: {
        data: [
          {type: 'max', name: 'Max'},
          {type: 'min', name: 'Min'}
        ]
      },
      markLine: {
        data: [
          {type: 'average', name: 'Avg'},
          [
            {
              symbol: 'none',
              x: '90%',
              yAxis: 'max'
            },
            {
              symbol: 'circle',
              label: {
                position: 'start',
                formatter: 'Max'
              },
              type: 'max',
              name: '最高点'
            }
          ]
        ]
      }
    }
  ]
};

export default {
  name: "ChartsManage",
  data() {
    return {}
  },
  mounted() {
    // 饼图
    let pieDom = document.getElementById('pie');
    let pieChart = echarts.init(pieDom);
    charts().then(res => res.data).then(res => {
      // 饼图
      option1.series[0].data = res.data?.bar || []
      pieChart.setOption(option1)
    })

    // 折线图
    let linetDom = document.getElementById('line');
    let lineChart = echarts.init(linetDom);
    axios.get('https://v1.yiketianqi.com/free/week?unescape=1&appid=15586572&appsecret=z57KIOL9').then((res) => {
      console.log(res.data.data)
      option2.xAxis.data = res.data.data?.map(v => v.date)
      option2.series[0].data = res.data.data?.map(v => v.tem_day)
      option2.series[1].data = res.data.data?.map(v => v.tem_night)
      lineChart.setOption(option2)
    })
  }
}
</script>

<style scoped>

</style>